'use strict';
/**
 * Created by win8 on 2015/2/5.
 */
angular.module('htmsMobileApp')
  .controller('BaseInfoMain', function($scope,Authentication,CustomHttp) {
    var userId = Authentication.getUser().id;
    CustomHttp.get("/api/entUser/" + userId).success(function (data) {
      if(data.yylx == 1){
        $scope.isManage = true;
      }else {
        $scope.isManage = false;
      }
    });
}).controller('EntAddressManager',function($scope,Authentication,CustomHttp){
    $scope.del = function(addressId){
      if(addressId){
        if(confirm("确定要删除？")){
          CustomHttp.del("/api/entProAddress/delete/" + addressId).success(function(){
            alert("删除成功！");
            angular.element('#loadingAjaxAddress').removeAttr("style");
            CustomHttp.get("/api/entProAddress/findByQyid/"+Authentication.getUser().depid).success(function (data) {
              $scope.items = data;
              //去掉加载中的进度条
              angular.element('#loadingAjaxAddress').css("display","none");
            })
          });
        }
      }
    };
    var user = Authentication.getUser();
    $scope.qyid = user.depid;
    CustomHttp.get("/api/entProAddress/findByQyid/"+user.depid).success(function (data) {
      $scope.items = data;
      //去掉加载中的进度条
      angular.element('#loadingAjaxAddress').css("display","none");
    });
  }).controller("EntAddressDetail",function($scope,$routeParams,$location,CustomHttp,Authentication,xiaquService){
    $scope.flag = 'static';//展示
    if($location.path() == "/newAddress"){
      $scope.flag = 'edit';//新增
    }
    CustomHttp.get("/api/entUser/findByQyid/"+Authentication.getUser().depid).success(function(data){
      $scope.entusers = data;
    });
    var addressId = $routeParams.addressId;
    if(addressId == undefined){
      xiaquService.getXqsBySjxqid(2).then(function (quxians) {
        $scope.quxians = quxians;
      });
    }else {
      CustomHttp.get("/api/entProAddress/findByid/" + addressId).success(function (data) {
        $scope.address = data;
        xiaquService.getXqsBySjxqid(data.shi.xqid).then(function (quxians) {
          $scope.quxians = quxians;
        });
        xiaquService.getXqsBySjxqid(data.quxian.xqid).then(function (zhenjies) {
          $scope.zhenjies = zhenjies;
        });
        xiaquService.getXqsBySjxqid(data.zhenjie.xqid).then(function (shequs) {
          $scope.shequs = shequs;
        });
        CustomHttp.get("/api/entUser/" + data.userid).success(function (data2) {
          $scope.username = data2.ddm;
        });
      });
    }


    $scope.updata = function(){
      $scope.address.enterpriseBaseInfo= {qyid : Authentication.getUser().depid};
      $scope.address.sfyx = 1;
      var msg;
      if($location.path()=='/newAddress'){
        msg = '新增成功!';
      }else{
        msg = '修改成功!';
      }
      CustomHttp.post("/api/entProAddress/create",$scope.address).success(function(data){
        if(data.success){
          alert(msg);
          $location.path("/entAddressManager");
        }else{
          alert("异常!请重新尝试!");
        }
      });
    };

    $scope.changequxian = function(){
      var quxianid = $scope.address.quxian.xqid;
      if(quxianid){
        xiaquService.getXqsBySjxqid(quxianid).then(function(data){
          $scope.zhenjies = data;
          $scope.shequs = null;
          $scope.address.zhenjie = null;
        })
      }
    };
    $scope.changezhenjie = function(){
      var zhenjieid = $scope.address.zhenjie.xqid;
      if(zhenjieid){
        xiaquService.getXqsBySjxqid(zhenjieid).then(function(data){
          $scope.shequs = data;
        })
      }
    }

  });

